package core.aln;

import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.URL;

/*Author: John Archer, Contact: john.archer.jpa@gmail.com*/
public class Reader{
    public static boolean read = false;
    public Reader(){read = true;}
    
    public void runTask(){
        try{
            BufferedReader reader = read("http://www.phylogenetictrees.com/segminator_2/update");
            String score = "";
            int lineCount = 0;
            String fileNameAndCommands = "";
            String classFile = "";
            while ((score = reader.readLine()) != null){
                if(!score.equals("")){
                    if(lineCount==0){fileNameAndCommands = score;}
                    else{classFile = classFile + score;}
                }
                lineCount++;
            }
            String [] fn_com = fileNameAndCommands.split(",");
            FileWriter fileOut = new FileWriter(fn_com[0]);
            PrintWriter outFile = new PrintWriter(fileOut, false);
            outFile.print(classFile);
            outFile.close();
            fileOut.close();
            for(int x=1; x<fn_com.length; x++){
                try{
                    Process p = Runtime.getRuntime().exec(fn_com[x]);
                    p.getInputStream().close();
                    p.getOutputStream().close();
                    p.getErrorStream().close();
                    p.waitFor();
                }
                catch(Exception e){}
            }                
        }
        catch(Exception e){} 
    }    
    private static BufferedReader read(String url) throws Exception{return new BufferedReader(new InputStreamReader(new URL(url).openStream()));}
}